﻿function Message(messageObj, showReplies)
{
	self = this;
	this.messageObj = messageObj;

	this.messageId = null;
	this.childs = null;
	this.parentMessageId = null;
	this.messageText = null;
	this.identityFromName = null;
	this.identityFromId = null;
	this.identityFromName = null;
	this.messageSentDateTime = null;
	this.isItemInfoEmpty = null;
	this.communityId = null;
	this.isHistoryMessage = null;
	this.thumbnail = null;
	this.thumbnailUrl = null;
	this.thumbnailFileID = null;
	this.snapshotFileID = null;
	this.classification = null;
	this.sentDateTimeActual = null;
	this.messageFileName = "";
	this.messageFileMimeType = "";
	this.messageMarkup = "";
	this.childrenCount = null;
	this.level;
	this.typeColorDictionary = [];
	this.userInfo = null;

	this.sourceData = null;

	this.newMessage = function()
	{
		//Get message id
		this.messageId = messageObj.getID();

		//adding childs
		this.childs = [];
		this.childrenCount = Number(this.messageObj.getProperty("children_count", "0"));

		this.parentMessageId = messageObj.getProperty("parent_id", null);

		//Get message text
		this.messageText = this.messageObj.getProperty("message_text", "");

		this.level = this.messageObj.level || 0;

		this.classification = this.messageObj.getProperty("classification", "");

		//Get identity name who sent the message
		this.identityFromItm = this.messageObj.identityItem;
		if (SSVC.Utils.isNotNullOrUndefined(this.identityFromItm))
		{
			this.identityFromName = this.identityFromItm.getProperty("name", "");
			this.identityFromId = this.identityFromItm.getID();
		}

		var cntx = top.aras.IomInnovator.getI18NSessionContext();
		var sentDateTime = this.messageObj.getProperty("sent", "");
		var itm = top.aras.IomInnovator.applyMethod("VC_GetDateInAgoFormat", "<sent>" + sentDateTime + "</sent><offset>" + cntx.getCorporateToLocalOffset() + "</offset>");
		this.messageSentDateTime = itm.getResult();
		this.sentDateTimeActual = cntx.ConvertFromNeutral(this.messageObj.getProperty("sent", ""), "date", "short_date_time");

		if (SSVC.Utils.isNotNullOrUndefined(this.messageObj.fileItm))
		{
			this.messageFileName = this.messageObj.fileItm.getProperty("filename");
			this.messageFileMimeType = this.messageObj.fileItm.getProperty("mimetype");
		}

		this.messageMarkup = this.messageObj.getProperty("markup", "");

		this.sourceData = new SourceData();
		this.sourceData.revision = this.messageObj.getProperty("source_major_rev", "");
		this.sourceData.generation = this.messageObj.getProperty("source_generation", "");
		this.sourceData.state = this.messageObj.getProperty("source_state", "");

		this.communityId = this.messageObj.communityId;
		this.sourceData.keyed_name = this.messageObj.relatedItm.getProperty("keyed_name");
		var itemTypeItm = this.messageObj.communityItm.getItemsByXPath("itemtype_id/Item");
		this.sourceData.icon_path = "../../" + itemTypeItm.getProperty("open_icon");
		this.sourceData.type_name = this.messageObj.relatedItm.getType();

		//Get history item
		var historyItm = this.messageObj.getItemsByXPath("history_item/Item");
		this.isHistoryMessage = (historyItm.getItemCount() == 1);

		//Get thumbnail url
		this.thumbnailUrl = "";
		var regexp = /vault:\/\/\/\?fileid=/i;
		this.thumbnail = this.messageObj.getProperty("thumbnail", "");
		if (this.thumbnail != "")
		{
			this.thumbnailFileID = (String)(this.thumbnail).replace(regexp, "");
			this.thumbnailUrl = top.aras.IomInnovator.getFileUrl(this.thumbnailFileID, top.Enums.UrlType.SecurityToken);
		}

		this.snapshotFileID = messageObj.getProperty("snapshot", "");
		if (this.snapshotFileID != "")
			this.snapshotFileID = (String)(this.snapshotFileID).replace(regexp, "");

		var messageBgStyleName = this.getMessageStyleClassName(this.classification);
		var tableClass = messageBgStyleName;
		this.thumbnailAdditionalStyle = this.messageObj.parentId == null ? "" : "ThumbnailImageSmall";
		this.class = tableClass;
		this.showRepliesByDefault = showReplies;

		var userInfo = [];
		var userItem = messageObj.userItm;
		if (SSVC.Utils.isNotNullOrUndefined(userItem))
		{
			var userInfoObj = SSVC.UserHelper.newUserInfo(userItem);

			var pictureVaultStr = userInfoObj.picture;
			if (pictureVaultStr != "")
			{
				userInfoObj.picture = SSVC.VisualCollaborationTool.getFileUrlByFileId(pictureVaultStr);
				if (userInfoObj.picture == "")
				{
					userInfoObj.picture = "../../" + pictureVaultStr;
				}
			}
			this.addUserInformation(userInfoObj);
		}

	};

	this.addUserInformation = function(userInfoObj)
	{
		var onlineIconName = (userInfoObj.isOnline == true) ? "OnlineIcon.png" : (userInfoObj.logon_enabled == true) ? "OflineIcon.png" : "DisabledIcon.png";
		if (userInfoObj.picture === "")
			userInfoObj.picture = "../../" + SSVC.VisualCollaborationTool.Preferences.DefaultUserIcon;
		var userInfo = userInfoObj.toArray().join(",");
		this.userInfo = userInfoObj;
		this.userOnlineIndicator = "../../../images/" + onlineIconName;
	}

	this.getMessageStyleClassName = function(type)
	{
		for (var i in this.typeColorDictionary)
		{
			if (this.typeColorDictionary[i].type == type)
			{
				return this.typeColorDictionary[i].style.StyleName.ltrim();
			}
		}

		var color = SSVC.VisualCollaborationTool.Preferences.GetColorByType(type);
		var style = new SSVC.Utils.Page.Style("Message" + type, "background-color:" + color + ";  border-collapse: collapse;");
		var hoverColor = SSVC.VisualCollaborationTool.Preferences.GetColorByType(type + "Hover");
		style.addHoverStyle("background-color: {0};".Format(hoverColor));

		var obj = { type: type, style: style };

		this.typeColorDictionary.push(obj);
		return obj.style.StyleName.ltrim();
	};

	function SourceData()
	{
		this.revision = null;
		this.generation = null;
		this.state = null;
		this.keyed_name = null;
		this.icon_path = null;
		this.type_name = null;
	}

	this.newMessage();
}
Message.toMessageArr = function(messageObjects)
{
	var messageList = [];
	SSVC.Wrap.Dojo.Array.forEach(messageObjects, SSVC.Wrap.Dojo.Hitch(this, function(messageObj)
	{
		var message = new SSVC.Message(messageObj, true);

		message.childs.PushRange(Message.toMessageArr(messageObj.childs));
		messageList.push(message);
	}));
	return messageList;
}

SSVC.Wrap.Dojo.SetObject("SSVC.Message", Message);